package online.westonline.com.westonline.ui;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easy.west.westclient.api.meeting.bean.MeetRoomBean;
import com.easy.west.westclient.api.meeting.event.FindMeetingRoomLsitEvent;
import com.hhl.library.FlowTagLayout;
import com.hhl.library.OnTagSelectListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.qqtheme.framework.picker.DatePicker;
import online.westonline.com.westonline.MyApplication;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.meeting.MeetListAdpter;
import online.westonline.com.westonline.adapter.tag.TagAdapter;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.entry.Tag;
import online.westonline.com.westonline.utils.ToastUtils;
import online.westonline.com.westonline.utils.UiUtlis;

public class MeetingActivity extends BaseActivity {


    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;
    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.li_meeting_select_date)
    LinearLayout liMeetingSelectDate;
    @BindView(R.id.recyclerView_meetinglist)
    RecyclerView recyclerViewMeetinglist;
    @BindView(R.id.tag_time_layout)
    FlowTagLayout tagTimeLayout;
    @BindView(R.id.tag_type_layout)
    FlowTagLayout tagTypeLayout;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tag_vip_layout)
    FlowTagLayout tagVipLayout;
    @BindView(R.id.tag_all_layout)
    FlowTagLayout tagAllLayout;
    @BindView(R.id.li_type)
    LinearLayout liType;
    private List<Tag> time_tag_list = new ArrayList<>();
    private List<Tag> type_tag_list = new ArrayList<>();
    private List<Tag> vip_tag_list = new ArrayList<>();
    private List<Tag> all_tag_list = new ArrayList<>();
    private TagAdapter<Tag> time_tagAdapter;
    private TagAdapter<Tag> type_tagAdapter;
    private TagAdapter<Tag> vip_tagadpter;
    private TagAdapter<Tag> all_tagadpter;
    private List<MeetRoomBean> meetRoomBeanList = new ArrayList<>();
    //时间框框
    private DatePicker datePicker;
    private int now_month_day;
    private int now_month;
    private int now_year;
    private int select_year;
    private int select_month;
    private int select_month_day;
    private String selcet_date;
    private int size;
    private int type;
    private int select_time;
    private boolean isvip = false;
    private boolean allcheck = false;
    private boolean time_select = false;
    private MeetListAdpter meetListAdpter;


    @Override
    protected void initview() {
        //初始化toolbar
        toolbar.setBackgroundColor(UiUtlis.getColor(R.color.colorAppTheme));
        toolbar.setTitle("会议室预定");
        toolbar.setTitleTextColor(UiUtlis.getColor(R.color.colorMenuBtn));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        liType.setVisibility(View.GONE);
        //初始化tag
        inittag();
        //初始化列表
        initrecyclerview();
        //初始化事件
        initsession();

    }

    /**
     * 初始化列表
     */
    private void initrecyclerview() {
        meetListAdpter = new MeetListAdpter();
        recyclerViewMeetinglist.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
        recyclerViewMeetinglist.setAdapter(meetListAdpter);
        meetListAdpter.setOnMeetingListener(new MeetListAdpter.onMeetingListener() {
            @Override
            public void onclick(int pos, MeetRoomBean roomBean) {
                Bundle b = new Bundle();
                b.putSerializable("data",roomBean);
                b.putString("date",selcet_date);
                b.putInt("time",select_time);
                startActivityWithBundle(OrederMeetActivity.class,false,b);
            }
        });
    }


    /**
     * 初始化tag
     */
    private void inittag() {
        all_tagadpter = new TagAdapter<>(this);
        vip_tagadpter = new TagAdapter<>(this);
        time_tagAdapter = new TagAdapter<>(this);
        type_tagAdapter = new TagAdapter<>(this);
        all_tag_list.add(new Tag(0, "全部"));
        vip_tag_list.add(new Tag(0, "vip"));
        vip_tagadpter.onlyAddAll(vip_tag_list);
        all_tagadpter.onlyAddAll(all_tag_list);
        time_tag_list.add(new Tag(1, "上午(8:30-12:00)"));
        time_tag_list.add(new Tag(2, "下午(14:00-18:00)"));
        time_tag_list.add(new Tag(3, "晚上(18:30-22:00)"));
        time_tag_list.add(new Tag(4, "全天(8:30-22:00)"));
        time_tagAdapter.onlyAddAll(time_tag_list);
        tagTimeLayout.setAdapter(time_tagAdapter);
        time_tagAdapter.notifyDataSetChanged();
        type_tag_list.add(new Tag(1, "大型"));
        type_tag_list.add(new Tag(2, "中型及以下"));
        type_tagAdapter.onlyAddAll(type_tag_list);
        tagTypeLayout.setAdapter(type_tagAdapter);
        type_tagAdapter.notifyDataSetChanged();
        tagAllLayout.setAdapter(all_tagadpter);
        tagVipLayout.setAdapter(vip_tagadpter);
        vip_tagadpter.notifyDataSetChanged();
        all_tagadpter.notifyDataSetChanged();
        tagTimeLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        tagTypeLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        tagVipLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
        tagAllLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);

    }

    /**
     * 事件初始化
     */
    private void initsession() {
        //时区选择
        tagTimeLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() > 0) {
                    //因为是单选
                    select_time = time_tag_list.get(selectedList.get(0)).getId();
                    time_select = true;
                    liType.setVisibility(View.VISIBLE);

                } else {
                    select_time = 0;
                    liType.setVisibility(View.GONE);
                    time_select = false;
                    recyclerViewMeetinglist.removeAllViews();
                }
            }
        });
        tagTypeLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() > 0) {
                    size = type_tag_list.get(selectedList.get(0)).getId();
                    if (time_select) {

                        //请求
                        MyApplication.getClient().findMeetingRoomBy(selcet_date,
                                select_time, size, isvip);
                    }

                } else {
                    size = 0;
                    //请求
                    MyApplication.getClient().findMeetingRoomBy(selcet_date,
                            select_time, size, isvip);
                }
                /*
                if (selectedList.size() > 0) {
                    type_tagAdapter.setPostionSelcted(selectedList.get(0));
                    type_tagAdapter.notifyDataSetChanged();
                }
                */
                tagAllLayout.clearAllOption();
                allcheck=false;
            }
        });
        //全部
        tagAllLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() > 0) {
                    //清除其他类型的选择状态
                    all_tagadpter.setPostionSelcted(0);
                    all_tagadpter.notifyDataSetChanged();
                    allcheck = true;
                    if (time_select) {
                        //查询全部
                        MyApplication.getClient().findMeetingRoomBy(selcet_date,
                                select_time, 0, isvip);
                    }
                } else {
                    allcheck = false;
                    //请求
                    MyApplication.getClient().findMeetingRoomBy(selcet_date,
                            select_time, size, isvip);
                }

                tagTypeLayout.clearAllOption();
                tagVipLayout.clearAllOption();
                //清空其他类型
                isvip = false;
                size = 0;

            }
        });
        //vip
        tagVipLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                if (selectedList.size() == 1) {
                    vip_tagadpter.setPostionSelcted(0);
                    vip_tagadpter.notifyDataSetChanged();
                    isvip = true;
                    //查询全部
                    MyApplication.getClient().findMeetingRoomBy(selcet_date,
                            select_time, size, isvip);
                } else {
                    isvip = false;
                    //请求
                    MyApplication.getClient().findMeetingRoomBy(selcet_date,
                            select_time, size, isvip);
                }

                tagAllLayout.clearAllOption();
                allcheck=false;
            }
        });
        //时间点击事件
        liMeetingSelectDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                datePicker.show();

            }
        });
        //时间选择事件
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
            @Override
            public void onDatePicked(String year, String month, String day) {
                select_year = Integer.parseInt(year);
                select_month = Integer.parseInt(month);
                select_month_day = Integer.parseInt(day);
                if (select_year < now_year) {
                    ToastUtils.showToast("不能在过去创建计划");
                    return;
                }
                if (select_month == now_month) {
                    if (select_month_day < now_month_day) {
                        ToastUtils.showToast("不能在过去创建计划");
                        return;
                    }
                }
                if (select_year == now_year) {
                    if (select_month < now_month) {
                        ToastUtils.showToast("不能在过去创建计划");
                        return;
                    }
                }
                selcet_date = year+month+day;
                tvDate.setText(select_year + "-" + select_month + "-" + select_month_day);
            }
        });
    }

    //设置列表
    private void configRecyclerview() {
        meetListAdpter.setDatas(meetRoomBeanList);
        meetListAdpter.notifyDataSetChanged();
    }

    @Override
    protected void initdata() {
        datePicker = new DatePicker(MeetingActivity.this);
        //今日
        now_month_day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
        //现在月份
        now_month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        //当前年份
        now_year = Calendar.getInstance().get(Calendar.YEAR);
        tvDate.setText(now_year + "-" + now_month + "-" + now_month_day);
        selcet_date = now_year + "-" + now_month + "-" + now_month_day;
        datePicker.setRangeStart(now_year, now_month, now_month_day);
        datePicker.setRangeEnd(now_year, 12, 31);
        datePicker.setSelectedItem(now_year, now_month, now_month_day);
        datePicker.setLineSpaceMultiplier(4);

    }

    @Override
    protected int setlayout() {
        return R.layout.activity_meeting;
    }

    /**
     * 获取会议室
     *
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getMeetingRoom(FindMeetingRoomLsitEvent eva) {
        if (eva.isOk()) {
            meetRoomBeanList = eva.getBean().getData();
            configRecyclerview();
        }
    }


}
